package com.autoswf.automator
{
	import flash.events.StatusEvent;
	import flash.net.LocalConnection;
	
	public class LCAutomationClient implements IAutomationClient
	{
		protected var lc:LocalConnection;
		protected var _remoteLoadCommand:IRemoteLoadCommand;
		protected var _automationClient:IAutomationClient;
		
		protected var _txId:String = "_autoswf.automationTx";
		protected var _rxId:String = "_autoswf.automationRx";
		
		/**
		 * Constructor.
		 */
		public function LCAutomationClient(automationClient:IAutomationClient = null)
		{
			_automationClient = automationClient;
			
			lc = new LocalConnection();
			lc.addEventListener(StatusEvent.STATUS, lc_statusEventHandler);
			lc.client = this;
			lc.connect(_rxId);
		}

		/**
		 * Invoked by console over local connection.
		 */		
		public function lc_remoteLoadCommand(descriptor:String):void
		{
			_remoteLoadCommand.execute(descriptor);  /* send to harness to interpret */
		}

		/**
		 * [IAutomationClient method]
		 * Broadcast a discriptor over local connection. 
		 */
		public function notify(descriptor:String):void
		{
			lc.send(_txId, "lc_notify", descriptor);
			
			if(_automationClient)
			{
				_automationClient.notify(descriptor);
			}
		}
		
		/**
		 * [IAutomationClient method]
		 * Receive a descriptor over local connection.
		 */
		public function set remoteLoadCommand(value:IRemoteLoadCommand):void
		{
			_remoteLoadCommand = value;
		}
		
		protected function lc_statusEventHandler(event:StatusEvent):void
		{
//			trace("Sending on LC: \""+_connectionId+"\" failed, receiving connection probably wasn't opened yet.");
		}
		
	}
}